package com.losdun.aftersales.service.impl;

import com.losdun.aftersales.domain.ContractOrder;
import com.losdun.aftersales.enums.OrderStatusEnum;
import com.losdun.aftersales.liteweb.LitewebClient;
import com.losdun.aftersales.liteweb.vo.TaskVo;
import com.losdun.aftersales.liteweb.vo.req.TaskQueryReq;
import com.losdun.aftersales.manager.NumberGenerator;
import com.losdun.aftersales.mapper.ContractOrderMapper;
import com.losdun.aftersales.service.IContractOrderService;
import com.losdun.common.annotation.DataScope;
import com.losdun.common.utils.DateUtils;
import com.losdun.common.utils.spring.SpringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;


/**
 * 合同订单Service业务层处理
 * 
 * @author swordye
 * @date 2025-08-19
 */
@Service
public class ContractOrderServiceImpl implements IContractOrderService 
{
    @Autowired
    private ContractOrderMapper contractOrderMapper;
    @Autowired
    private LitewebClient litewebClient;
    @Autowired
    private NumberGenerator numberGenerator;

    /**
     * 查询合同订单
     * 
     * @param id 合同订单主键
     * @return 合同订单
     */
    @Override
    public ContractOrder selectContractOrderById(Long id)
    {
        ContractOrder order = contractOrderMapper.selectContractOrderById(id);
        //order.setStepList(queryRemoteStep(order.getOrderNo()));
        return order;
    }

    private List<TaskVo> queryRemoteStep(String orderNo) {
        TaskQueryReq req=new TaskQueryReq();
        req.setProjectCode(orderNo);
        return litewebClient.queryTaskList(req);
    }

    /**
     * 查询合同订单列表
     * 
     * @param contractOrder 合同订单
     * @return 合同订单
     */
    @Override
    public List<ContractOrder> selectContractOrderList(ContractOrder contractOrder)
    {
        List<ContractOrder> list = contractOrderMapper.selectContractOrderList(contractOrder);
//        list.forEach(t->{
//            t.setStepList(queryRemoteStep(t.getOrderNo()));
//        });
        return list;
    }

    @Override
    public List<ContractOrder> selectAuthOrderList(ContractOrder contractOrder){
        return SpringUtils.getAopProxy(this).selectAuthContractOrderList(contractOrder);
    }

    @DataScope(userAlias = "u")
    public List<ContractOrder> selectAuthContractOrderList(ContractOrder contractOrder){
        return contractOrderMapper.selectAuthContractOrderList(contractOrder);
    }

    /**
     * 新增合同订单
     * 
     * @param contractOrder 合同订单
     * @return 结果
     */
    @Override
    public int insertContractOrder(ContractOrder contractOrder)
    {
        if(contractOrder.isMember()){
            contractOrder.setOrderNo(numberGenerator.generateNo("PO"));
        }else{
            contractOrder.setOrderNo(numberGenerator.generateNo("GO"));
        }
        contractOrder.setCreateTime(DateUtils.getNowDate());
        return contractOrderMapper.insertContractOrder(contractOrder);
    }

    /**
     * 修改合同订单
     * 
     * @param contractOrder 合同订单
     * @return 结果
     */
    @Override
    public int updateContractOrder(ContractOrder contractOrder)
    {
        ContractOrder old = contractOrderMapper.selectContractOrderById(contractOrder.getId());

        if(!old.getStatus().equals(contractOrder.getStatus())){
            if(OrderStatusEnum.AUDIT_PASS.getValue().equals(contractOrder.getStatus())){
                contractOrder.setDesignDate(LocalDate.now());
            }else if(OrderStatusEnum.PRODUCTING.getValue().equals(contractOrder.getStatus())){
                contractOrder.setFactoryDate(LocalDate.now());
            }else if(OrderStatusEnum.SHIPPING.getValue().equals(contractOrder.getStatus())){
                contractOrder.setShippingDate(LocalDate.now());
            }else if(OrderStatusEnum.RECEIVED.getValue().equals(contractOrder.getStatus())){
                contractOrder.setReceivedDate(LocalDate.now());
            }
        }
        contractOrder.setUpdateTime(DateUtils.getNowDate());
        return contractOrderMapper.updateContractOrder(contractOrder);
    }

    /**
     * 批量删除合同订单
     * 
     * @param ids 需要删除的合同订单主键
     * @return 结果
     */
    @Override
    public int deleteContractOrderByIds(Long[] ids)
    {
        return contractOrderMapper.deleteContractOrderByIds(ids);
    }

    /**
     * 删除合同订单信息
     * 
     * @param id 合同订单主键
     * @return 结果
     */
    @Override
    public int deleteContractOrderById(Long id)
    {
        return contractOrderMapper.deleteContractOrderById(id);
    }

    @Override
    public List<ContractOrder> queryUnShippingOrder() {
        return contractOrderMapper.queryUnShippingOrder();
    }

    @Override
    public void updateStatus(Long id, Integer status, LocalDate statusDate) {
        contractOrderMapper.updateStatus(id, status, statusDate);
    }

    @Override
    public void orderConfirmReceived(Long id) {
        updateStatus(id, OrderStatusEnum.RECEIVED.getValue(), LocalDate.now());
    }

    @Override
    public List<ContractOrder> queryCompletedContactOrder() {
        return contractOrderMapper.selectCompleteOrderList(LocalDate.now().plusMonths(-12), LocalDate.now().plusMonths(-11));
    }

    @Override
    public void updateMsgSendStatus(Long orderId) {
        contractOrderMapper.updateMsgSendStatus(orderId);
    }
}
